package midtool;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jfugue.midi.MidiFileManager;
import org.jfugue.pattern.Pattern;
import org.jfugue.player.Player;
import org.staccato.ReplacementMapPreprocessor;
import org.staccato.maps.SolfegeReplacementMap;

import com.attilax.collection.listBuilder;
import com.attilax.util.timestampUtil;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

public class jyepuUtil {
	
	public static 	 Map rules = new HashMap() {{
         put("1", "c");
         put("2", "d");
         put("3", "e");
         put("4", "f");
         put("5", "g");
         put("6", "a");
         put("7", "b"  );
         put("H", "H"  );
        
   }};
	public static void main(String[] args) throws IOException {
		
		
	    
	    
		//ReplacementMapPreprocessor rmp = ReplacementMapPreprocessor.getInstance();
	//	rmp.setReplacementMap(rules).setRequireAngleBrackets(false);
		//Player player = new Player();
		//player.play(new Pattern("do re mi fa so la ti do")); // This will play "C D E F G A B"
		

		// This next example brings back the brackets so durations can be added
	//	rmp.setRequireAngleBrackets(true);
	//	player.play(new Pattern("<Do>q <Re>q <Mi>h | <Mi>q <Fa>q <So>h | <So>q <Fa>q <Mi>h | <Mi>q <Re>q <Do>h"));
		
		
		Pattern Pattern1 = new Pattern();   
		String s="  1155665-  4433221-  5544332-    5544332-     1155665-    4433221-";//  5544332    5544332     1155665    4433221
		listBuilder listBuilder = strService.$(s).replace("    aaa","\\|").toList().trimElement();
		List li=processToneLen(listBuilder.liObj,rules);
	//	String s2=listBuilder.join(" ").build();
		String joinSpaceAfted = Joiner.on(" ").join(li);
		
		
		String ins="Telephone_Ring";
		String insTmplt = "I[@ins@] ";
		insTmplt=insTmplt.replaceAll("@ins@", ins);
		joinSpaceAfted=insTmplt+ joinSpaceAfted;
		
		joinSpaceAfted="E5ia95d0 B5ia95d0 A5ia95d0 B5ia95d0 A5ia95d0 A5ia95d0 B5ia95d0";//dinsyohwer 
		System.out.println(joinSpaceAfted);
		Pattern1.add(joinSpaceAfted);  
		String asTimeStamp_millsec_filenameencode = timestampUtil.join("c:\\0log\\atifirstmid-@ins@-@tmstmp@.mid").asTimeStamp_millsec_filenameencode();
		
		asTimeStamp_millsec_filenameencode=asTimeStamp_millsec_filenameencode.replaceAll("@ins@", ins);
		File file = new File( asTimeStamp_millsec_filenameencode );
	 	new MidiFileManager().savePatternToMidi(Pattern1, file);
	// 	Player player = new Player();
	 //	player.play(Pattern1);
		System.out.println("--");
	}
	public static String getMidiStr(String s) {
		listBuilder listBuilder = strService.$(s).replace("    aaa","\\|").toList().trimElement();
		List li=processToneLen(listBuilder.liObj,rules);
	//	String s2=listBuilder.join(" ").build();
		String joinSpaceAfted = Joiner.on(" ").join(li);
		return joinSpaceAfted;
	}
	private static List processToneLen(List liObj, Map rules) {
		 List li=Lists.newArrayList();
		String lasttone;
		for (Object object : liObj) {
			String tone=(String) object.toString().trim();
			if(tone.trim().length()==0)
				continue;
			if(!tone.equals("-") )
			{
				String trueTone=(String) rules.get(tone);
				li.add(trueTone);continue;
			}
				
			if(tone.equals("-") )
			{
				int indexlast = li.size()-1;
				String li_lasttone=(String) li.get(indexlast);
				String newtone=li_lasttone+"5H";
				li.remove(indexlast);
				li.add(newtone);
				continue;
			}
		}
		
		 
		return li;
	}
}